﻿using Minecraft.Config;
using Minecraft.Config.ipConst;
using Minecraft.Model;
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Config;
using SuperSocket.SocketBase.Protocol;
using System;

namespace Minecraft.ServerHall
{
	public class MinecraftServer : AppServer<MinecraftSession>
	{
		/// <summary>
		/// 服务器最后一次执行命令的时间（不包含心跳包）
		/// </summary>
		public DateTimeOffset lastCommandTime = DateTimeOffset.Now;
		/// <summary>
		/// 输出测试成员信息的间隔时间
		/// </summary>
		public long intervalSecond = 60;

		//结束符协议
		public MinecraftServer()
		: base(new TerminatorReceiveFilterFactory(SeparatorConfig.Protocol))
		{
			//Console.WriteLine("MinecraftServer");
		}
		[Obsolete]
		protected override void OnStartup()
		{
			//Console.WriteLine("OnStartup");
			base.OnStartup();
		}

		protected override void OnStarted()
		{
			//Console.WriteLine("OnStarted");
			base.OnStarted();
		}

		protected override void OnStopped()
		{
			//Console.WriteLine("OnStopped");
			base.OnStopped();
		}

		protected override bool Setup(IRootConfig rootConfig, IServerConfig config)
		{
			//Console.WriteLine("Setup");
			return base.Setup(rootConfig, config);
		}

		protected override void OnSystemMessageReceived(string messageType, object messageData)
		{
			//Console.WriteLine($"-----messageType:{messageType} messageData:{messageData.JsonSerialize()}");
			base.OnSystemMessageReceived(messageType, messageData);
		}

		protected override void OnNewSessionConnected(MinecraftSession session)
		{
			//Console.WriteLine($"新连接进入：{session.RemoteEndPoint.Address.ToString()}:{session.RemoteEndPoint.Port}");
			base.OnNewSessionConnected(session);
		}

		protected override void OnSessionClosed(MinecraftSession session, CloseReason reason)
		{
			//if (JsonConfig.Value.IsConsoleWrite)
			//{
			//	Console.WriteLine();
			//	Console.WriteLine($" OnSessionClosed 连接断开：{session.RemoteEndPoint.Address.ToString()}:{session.RemoteEndPoint.Port} 时间：【{DateTimeOffset.Now.ToStr()}】");
			//}
			base.OnSessionClosed(session, reason);
		}
		protected override bool RegisterSession(string sessionID, MinecraftSession appSession)
		{
			//Console.WriteLine($"注册Session：sessionID:{sessionID}  IPAddress:{appSession.RemoteEndPoint.Address.ToString()}:{appSession.RemoteEndPoint.Port}");
			return base.RegisterSession(sessionID, appSession);
		}

		protected override void ExecuteCommand(MinecraftSession session, StringRequestInfo requestInfo)
		{
			base.ExecuteCommand(session, requestInfo);
			EnumCommand command = ProtocolHelper.GetCommand(requestInfo.Key);

			if (JsonConfig.Value.IsConsoleWrite)
			{
				if (command != EnumCommand.Heart_HeartData)
				{
					lastCommandTime = DateTimeOffset.Now;
					intervalSecond = 60;
					string ipUserNameTipStr = IpConstConfig.GetIpUserNameTipStr(session.RemoteEndPoint.Address.ToString());
					Console.ForegroundColor = ConsoleColor.Yellow;
					Console.Write($"【服务器（{JsonConfig.Value.Name}）】执行命令：");
					if (!ipUserNameTipStr.IsNullOrWhiteSpace())
					{
						Console.ForegroundColor = ConsoleColor.Green;
					}
					else
					{
						Console.ForegroundColor = ConsoleColor.Magenta;
					}
					Console.WriteLine($"	{session.GetOutputStr(command)}");
					Console.ResetColor();
				}
			}
		}
	}
}
